var A = function () {
    return new Promise((resolve, inject) => {
        setTimeout(() => {
            console.log('A');
            resolve('A')
        }, 0)
    })
}

var B = function () {
    return new Promise((resolve, inject) => {
        setTimeout(() => {
            console.log('B');
            resolve('B')
            // inject('报错了')
        }, 5000)
    })
}
var C = function () {
    return new Promise((resolve, inject) => {
        setTimeout(() => {
            console.log('C');
            resolve('C')
        }, 500)
    })
}
var D = function () {
    return new Promise((resolve, inject) => {
        setTimeout(() => {
            console.log('D');
            resolve('D')
        }, 2000)
    })
}


async function name() {
    // var a = await A();
    // console.log("name：",a)
    try {
        var b = await B();
        console.log("name：", b)
        if (true) {
            console.log("ceshi0")
        }
    } catch (error) {
        console.log("ceshi0")
    }


    // var c = await C();
    // console.log("name：",c)
    // var d = await D();
    // console.log("name：",d)
    // console.log("ceshi")
}
// name()


// 构造一个延迟resolve的promise对象
let delayGet = value =>
    new Promise(resolve => setTimeout(() => resolve('resolve: ' + value), 1000))

// async把函数包装为Promise
// async内部await用于阻塞Promise
async function main() {
    console.time('main')

    console.log('pass0')

    // 获得promise
    let res1 = delayGet(1)
    let res2 = delayGet(2)

    console.log('pass1')

    // 等待promise.resolve
    console.log(await res1)
    console.log(await res2)

    console.log('pass2')

    console.log(await delayGet(1))
    console.log(await delayGet(2))

    console.log('pass3')

    console.timeEnd('main')

    return 200
}


console.log('start')
// 异步回调
main().then(res => console.log(res))

console.log('end')